package com.example.anydo.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.example.anydo.R;
import com.example.anydo.bean.ItemBean;
import com.example.anydo.bean.SubTaskBean;

import java.util.List;
//hxy create in 2021.06.21


public class ListViewAdapter extends ArrayAdapter<List<SubTaskBean.DataBean>> {

    private final int resId;
    private List<SubTaskBean.DataBean> mData;
    private Context mContext;

    ImageView done ;
    EditText title;
    ImageView delete ;
    Button add ;
    ImageView im_sub;


    public ListViewAdapter(Context mContext, List<SubTaskBean.DataBean> mData,int resId) {
        super(mContext,resId);
        this.mContext = mContext;
        this.mData = mData;
        this.resId = resId;
    }



    @Override
    public int getCount() {
        return mData.size() + 1;
    }


    @Override
    public long getItemId(int position) {
        return position;
    }


    private void showData(int position){
        im_sub.setVisibility(View.INVISIBLE);
        delete.setVisibility(View.VISIBLE);
        done.setVisibility(View.VISIBLE);
        add.setVisibility(View.INVISIBLE);
        title.setText(mData.get(position).getTitle());
        if ("是".equals(mData.get(position).getIfDone())){
            done.setImageResource(R.drawable.done_true);
        }else {
            done.setImageResource(R.drawable.done_false);
        }
    }

    private void showDddData(){
        im_sub.setVisibility(View.VISIBLE);
        delete.setVisibility(View.INVISIBLE);
        done.setVisibility(View.INVISIBLE);
        add.setVisibility(View.VISIBLE);
        title.setText("");
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View view = LayoutInflater.from(mContext).inflate(resId,parent,false);
        done = view.findViewById(R.id.iv_if_done);
        title = view.findViewById(R.id.et_sub);
        delete = view.findViewById(R.id.iv_delete);
        add = view.findViewById(R.id.btn_add);
        im_sub = view.findViewById(R.id.im_sub);
        if(position < mData.size()){
            showData(position);
        }else {
            showDddData();
        }

        done.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s_done = mData.get(position).getIfDone().equals("是") ? "否" : "是";
                mData.get(position).setIfDone(s_done);
                notifyDataSetChanged();
            }
        });

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mData.remove(position);
                notifyDataSetChanged();
            }
        });

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = title.getText().toString();
                if(!temp.equals("")){
                    mData.add(new SubTaskBean.DataBean(-1,-1,temp,"否"));
                }
                notifyDataSetChanged();
            }
        });
        return view;
    }


}